package handler

import (
	"AttackTracer/StingHoneypot/Stinghook/config"
	"bytes"
	"fmt"
	"io/ioutil"
)

var host_name string
var (
	ext_js_to_obfuscate     = ""
	ext_js_to_not_obfuscate = ""
)

//元素是否在数组内
func IsContain(items []string, item string) bool {
	for _, eachItem := range items {
		if eachItem == item {
			return true
		}
	}
	return false
}

func legacy_build_hookjs(host_name string) {
	hostjs := ""
	hostjs_path := "StingHoneypot/Stinghook/core/main/client"

	//External libraries (like jQuery) that are not evaluated with Eruby and possibly not obfuscated
	var ext_js_sub_files = []string{"lib/jquery-1.12.4.min.js", "lib/jquery-migrate-1.4.1.js", "lib/evercookie.js", "lib/json2.js", "lib/mdetect.js", "lib/platform.js", "lib/jquery.blockUI.js"}
	//BeEF libraries: need Eruby evaluation and obfuscation
	var beef_js_sub_files = []string{"beef.js", "browser.js", "browser/cookie.js", "browser/popup.js", "session.js", "os.js", "hardware.js", "dom.js", "logger.js", "net.js", "updater.js", "encode/base64.js", "encode/json.js", "net/local.js", "init.js", "mitb.js", "geolocation.js", "net/dns.js", "net/connection.js", "net/cors.js", "net/requester.js", "net/xssrays.js", "net/portscanner.js", "are.js"}

	/*
	   # @note Load websocket library only if WS server is enabled in config.yaml
	   if config.get("beef.http.websocket.enable") == true
	     beef_js_sub_files << "websocket.js"
	   end
	   # @note Load webrtc library only if WebRTC extension is enabled
	   if config.get("beef.extension.webrtc.enable") == true
	     beef_js_sub_files << "lib/webrtcadapter.js"
	     beef_js_sub_files << "webrtc.js"
	   end
	*/
	beef_js_sub_files = append(beef_js_sub_files, "timeout.js")

	var ext_js_sub_file_path bytes.Buffer

	for _, ext_js_sub_file := range ext_js_sub_files {
		if has, _ := config.Get("beef.http.websocket.enable"); has {
			//get找不到一个好的方式去实现，IsContain在上面
			has_core, str := config.Get("beef.extension.evasion.exclude_core_js")
			if has_core == true && ext_js_sub_file == str {
				fmt.Println("Excluding #{ext_js_sub_file} from core files obfuscation list")
				// do not obfuscate the file
				ext_js_sub_file_path.WriteString(hostjs_path)
				ext_js_sub_file_path.WriteString(ext_js_sub_file)

				dat, err := ioutil.ReadFile(ext_js_sub_file_path.String())
				if err != nil {
					panic(err)
				}
				ext_js_to_not_obfuscate = string(dat) + "\n\n"

			} else {
				ext_js_sub_file_path.WriteString(hostjs_path)
				ext_js_sub_file_path.WriteString(ext_js_sub_file)

				dat, err := ioutil.ReadFile(ext_js_sub_file_path.String())
				if err != nil {
					panic(err)
				}
				ext_js_to_obfuscate = string(dat) + "\n\n"
				//ext_js_to_obfuscate << (File.read(ext_js_sub_file_path) + "\n\n")
			}
		} else {
			//# Evasion is not enabled, do not obfuscate anything
			ext_js_sub_file_path.WriteString(hostjs_path)
			ext_js_sub_file_path.WriteString(ext_js_sub_file)
			dat, err := ioutil.ReadFile(ext_js_sub_file_path.String())
			if err != nil {
				panic(err)
			}
			ext_js_to_not_obfuscate = string(dat) + "\n\n"
		}
	}

}
